package org.cnbi.web.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cnbi.entity.Dashboard;
import org.cnbi.entity.Menu;
import org.cnbi.service.IHandleService;
import org.cnbi.utils.chart.ChartParamter;
import org.cnbi.utils.chart.json.JsonSeriesChart;
import org.cnbi.utils.date.DateUtil;
import org.cnbi.utils.file.FileUtil;
import org.cnbi.utils.pojo.FileBean;
import org.cnbi.utils.pojo.SqlParamsBean;
import org.cnbi.utils.pojo.UtilDim;
import org.cnbi.utils.tools.ActionConstants;
import org.cnbi.utils.tools.Constants;
import org.cnbi.utils.tools.MenuConstants;
import org.cnbi.web.controller.base.BaseController;
import org.cnbi.web.view.xml.ChartUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 系统请求页面的核心控制器 凡是是请求页面的动作，都到这里来写吧！ 
 * Copyright © 2014中国经邦. All rights reserved.
 * @author 龚佳新
 * @date 2014年4月17日下午12:47:04
 * @cnbi-web
 */
@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {
	
	private static final Log logger = LogFactory.getLog(ReportController.class);
	@Autowired
	private IHandleService handleService;
	
	@RequestMapping(value = "/index")
	public ModelAndView main(HttpServletResponse response) {
		ModelAndView view = firstNavHandle(MenuConstants.REPORT,response);
		report(view);
		return view;
	}
	private void report(ModelAndView view) {}
	@RequestMapping(value = "/"+MenuConstants.LEVELTWO)
	public ModelAndView levelTwo(SqlParamsBean param,Menu menu,HttpServletRequest request,HttpServletResponse response) {
		   ModelAndView view = secondNavHandle(menu, response);
		   Object datas = queryReport(param);
		   view.addObject(Constants.DATAS, datas);
		   view.addObject("reportJson", getServletContext().getAttribute("reportJson"));
		   dimMapHandle(view,param,menu);
		return view;
	}
	/**
	 * 
	 */
	private Object queryReport(SqlParamsBean param){
		List<FileBean> list = new ArrayList<FileBean>();
		String cym = param.getYear()+File.separator+param.getMonth()+File.separator+param.getCompany()+File.separator;
		String filePath = getRealPath("upload")+cym;//year/month/company
		System.out.println("filePath=="+filePath);
		File [] files = new File(filePath).listFiles();
		if(null != files)
		for(int i=0,len = files.length;i<len;i++){//
			    String fileName = files[i].getName();
			    String updateTime = DateUtil.formatDate(files[i].lastModified());
			    int index = fileName.lastIndexOf(".");
				String type = fileName.substring(index, fileName.length());
				if(!type.equals(".files") && !type.contains("htm")){
					fileName = fileName.substring(0,index);
					long aa = FileUtil.getFileSizes(files[i]);
					cym = cym.replace("\\", "/");
					String size = FileUtil.formatFileSize(aa);
					FileBean bean = new FileBean(fileName,size,DateUtil.getCurDate(""),updateTime,type,cym);
			        logger.info("类型为"+type+"----fileName=="+fileName+"--修改时间为----"+updateTime+"---大小为=="+aa);
			  	  list.add(bean);
				}
		}
	    return list;
	}
}
